package cn.yinyu.queue.module.ecg.controller.admin.queuesequence;

import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import java.util.*;
import java.io.IOException;

import cn.yinyu.queue.framework.common.pojo.PageParam;
import cn.yinyu.queue.framework.common.pojo.PageResult;
import cn.yinyu.queue.framework.common.pojo.CommonResult;
import cn.yinyu.queue.framework.common.util.object.BeanUtils;
import static cn.yinyu.queue.framework.common.pojo.CommonResult.success;

import cn.yinyu.queue.framework.excel.core.util.ExcelUtils;

import cn.yinyu.queue.framework.apilog.core.annotation.ApiAccessLog;
import static cn.yinyu.queue.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.yinyu.queue.module.ecg.controller.admin.queuesequence.vo.*;
import cn.yinyu.queue.module.ecg.dal.dataobject.queuesequence.QueueSequenceDO;
import cn.yinyu.queue.module.ecg.service.queuesequence.QueueSequenceService;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;

@Tag(name = "管理后台 - 当天序号")
@RestController
@RequestMapping("/ecg/queue-sequence")
@Validated
public class QueueSequenceController {

    @Resource
    private QueueSequenceService queueSequenceService;

    @PostMapping("/create")
    @Operation(summary = "创建当天序号")
    @PreAuthorize("@ss.hasPermission('ecg:doctor:task')")
    public CommonResult<Integer> createQueueSequence(@Valid @RequestBody QueueSequenceSaveReqVO createReqVO) {
        return success(queueSequenceService.createQueueSequence(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新当天序号")
    @PreAuthorize("@ss.hasPermission('ecg:doctor:task')")
    public CommonResult<Boolean> updateQueueSequence(@Valid @RequestBody QueueSequenceSaveReqVO updateReqVO) {
        queueSequenceService.updateQueueSequence(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除当天序号")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('ecg:doctor:task')")
    public CommonResult<Boolean> deleteQueueSequence(@RequestParam("id") Integer id) {
        queueSequenceService.deleteQueueSequence(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得当天序号")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('ecg:doctor:task')")
    public CommonResult<QueueSequenceRespVO> getQueueSequence(@RequestParam("id") Integer id) {
        QueueSequenceDO queueSequence = queueSequenceService.getQueueSequence(id);
        return success(BeanUtils.toBean(queueSequence, QueueSequenceRespVO.class));
    }

    @GetMapping("/get-timeslot-by-checktype")
    @Operation(summary = "获得当天未满的VIP时间段")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('ecg:doctor:task')")
    public CommonResult<List<QueueSequenceRespVO>> getTimeslotByCheckType(@RequestParam("checkType") Integer checkType) {
        List<QueueSequenceDO> queueSequenceList = queueSequenceService.selectGivenCheckTypeTimeslot(checkType);
        return success(BeanUtils.toBean(queueSequenceList, QueueSequenceRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得当天序号分页")
    @PreAuthorize("@ss.hasPermission('ecg:doctor:task')")
    public CommonResult<PageResult<QueueSequenceRespVO>> getQueueSequencePage(@Valid QueueSequencePageReqVO pageReqVO) {
        PageResult<QueueSequenceDO> pageResult = queueSequenceService.getQueueSequencePage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, QueueSequenceRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出当天序号 Excel")
    @PreAuthorize("@ss.hasPermission('ecg:doctor:task')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportQueueSequenceExcel(@Valid QueueSequencePageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<QueueSequenceDO> list = queueSequenceService.getQueueSequencePage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "当天序号.xls", "数据", QueueSequenceRespVO.class,
                        BeanUtils.toBean(list, QueueSequenceRespVO.class));
    }

}
